package cn.getech.data.development.controller.search;


import cn.getech.data.development.constant.StoreTableType;
import cn.getech.data.development.entity.TableInfo;
import cn.getech.data.development.entity.flink.BdpRealTableInfo;
import cn.getech.data.development.model.req.search.DataSearchReq;
import cn.getech.data.development.service.BdpRealTableInfoService;
import cn.getech.data.development.service.TableInfoService;
import cn.getech.data.intelligence.common.utils.Constant;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.model.dto.SysUserDto;
import cn.getech.system.center.service.SysUserService;
import cn.getech.system.center.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @description:  项目下的表接口
 * @author: wangzhaowen：kiss
 * @create: 2021/1/16 17:42
 * @version: 2021/1/16
 **/
@Api(tags = "项目下的表接口")
@RestController
@RequestMapping("/bdp/project/tableInfo")
public class DataProjectController {
    @Autowired
    private TableInfoService tableInfoService;

    @Autowired
    private BdpRealTableInfoService realTableInfoService;

    @Autowired
    private SysUserService sysUserService;


    @ApiOperation("获取未关联的数据表信息列表")
    @GetMapping("/listNoRelation")//  /bdp/project/tableInfo/listNoRelation
    @SysLog(value = "获取未关联项目的数据仓库列表", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R listNoRelation(@ApiParam(value = "{'table_name':'test','db_name':'test','proc_id':1,'table_type':1}")
                            @RequestParam Map<String, Object> params) {
        Integer page = 1;
        Integer limit = 10;
        int tableType = 1;
        if (params.get(Constant.PAGE) != null) {
            page = Integer.parseInt(params.get(Constant.PAGE).toString());
        }
        if (params.get(Constant.LIMIT) != null) {
            limit = Integer.parseInt(params.get(Constant.LIMIT).toString());
        }
        if (params.get("table_type") != null) {
            tableType = Integer.parseInt(params.get("table_type").toString());
        }
        params.put(Constant.PAGE,page);
        params.put(Constant.LIMIT,limit);
        params.put("create_per", ShiroUtils.getUserId().intValue());
        params.put("sidx", "id");
        params.put("order", "desc");
        if (StoreTableType.OFFLINE.getCode().equals(tableType)) {
            PageUtils<TableInfo> pageUtils = tableInfoService.listNoRelation(params);
            return R.okWithPage(pageUtils);
        }else{
            DataSearchReq param=new DataSearchReq();
            if (params.get("table_name") != null) {
                String table_name = params.get("table_name").toString();
                param.setTableName(table_name);
            }
            if (params.get("db_name") != null) {
                String db_name = params.get("db_name").toString();
                param.setDbName(db_name);
            }
            if (params.get("proc_id") != null) {
                Integer proc_id = Integer.parseInt(params.get("proc_id").toString());
                param.setNotProjectId(proc_id);
            }
            param.setPage(page);
            param.setLimit(limit);
            PageUtils<BdpRealTableInfo> pageUtils = realTableInfoService.queryPage(param);
            return R.okWithPage(pageUtils);
        }
    }

    @ApiOperation("用户列表")
    @GetMapping("/user/list") //   /bdp/project/tableInfo/user/list
    public R userList(@RequestParam(required = false, defaultValue = "0") Integer type,
                      Integer proc_id) {
        if (proc_id == null) {
            R.error("项目id不能为空");
        }
        List<Map<String, Object>> userList = realTableInfoService.getUserList(type, proc_id);
        return R.okWithData(userList);
    }

    /**
     * 获取已关联的数据表信息列表
     */
    @ApiOperation("获取已关联的数据表信息列表")
    @PostMapping("/list")// /bdp/project/tableInfo/list
    @SysLog(value = "获取已关联项目的数据仓库列表", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R list(@ApiParam(value = "{'table_name':'test','db_name':'test', 'proc_id':1}")
                  @RequestBody Map<String, Object> params) {
        Integer page = 1;
        Integer limit = 10;
        if (params.get(Constant.LIMIT) != null) {
            limit = Integer.parseInt(params.get(Constant.LIMIT).toString());
        }
        if (params.get(Constant.PAGE) != null) {
            page = Integer.parseInt(params.get(Constant.PAGE).toString());
        }
        int tableType = 1;
        if (params.get("table_type") != null) {
            tableType = Integer.parseInt(params.get("table_type").toString());
        }
        params.put(Constant.PAGE,page);
        params.put(Constant.LIMIT,limit);
        params.put("order", "desc");
        if (StoreTableType.OFFLINE.getCode().equals(tableType)) {
            PageUtils<TableInfo> pageUtils = tableInfoService.selectListPage(params);
            fillTableInfo(pageUtils.getList());
            return R.okWithPage(pageUtils);
        }else{
            DataSearchReq param=new DataSearchReq();
            if (params.get("table_name") != null) {
                String table_name = params.get("table_name").toString();
                param.setTableName(table_name);
            }
            if (params.get("createPerName") != null) {
                String createPerName = params.get("createPerName").toString();
                param.setCreatePerName(createPerName);
            }
            if (params.get("searchUserId") != null) {
                Long searchUserId = Long.parseLong(params.get("searchUserId").toString());
                param.setCreateUser(searchUserId);
            }
            if (params.get("db_name") != null) {
                String db_name = params.get("db_name").toString();
                param.setDbName(db_name);
            }
            if (params.get("proc_id") != null) {
                Integer proc_id = Integer.parseInt(params.get("proc_id").toString());
                param.setProjectId(proc_id);
            }
            param.setPage(page);
            param.setLimit(limit);
            PageUtils<BdpRealTableInfo> pageUtils = realTableInfoService.queryPage(param);
            return R.okWithPage(pageUtils);
        }


    }
    private void fillTableInfo(List<TableInfo> tableInfoList) {
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        for (TableInfo tableInfo : tableInfoList) {
            //添加创建用户的信息
            Long userId= Long.valueOf(tableInfo.getCreatePer());
            tableInfo.setCreatePerName(userMap.get(userId));
        }
    }
}

